<?php

namespace app\admin\controller\document;

use app\common\controller\Backend;

use think\Controller;
use think\Request;

/**
 * 文档
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Document模型对象
     */
    protected $model = null;
    protected $companyModel=null;
    protected $houseModel=null;
protected $expensesModel=null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Document');
        $this->companyModel=model('Company');
        $this->houseModel= model('House');
        $this->expensesModel=model('Expenses');
        $this->view->assign('company',$this->companyModel->where('companytype=1')->field('id,companyname')->select());
    }

    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name')) {
                return $this->selectpage();
            }
            return $this->handleSearch($this->searchfields);
        }
        return $this->view->fetch();
    }

    private function handleSearch($searchfields=null) {
        $append = array(
           
        );        
        list($where, $sort, $order, $offset, $limit,$orderParams) = $this->buildparams($searchfields,null,$append);
        $total = $this->model->where($where)->count();
        $list = $this->model->with('company')->where($where)->order($orderParams)->limit($offset, $limit)->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }

    public function add()
    {
        return parent::create('form');
    }

    public function print(){
        $id=isset($_GET['id']) ? $_GET['id'] : 0;
        $doc=$this->model->where("id",$id)->find();     
        if(empty($doc))
        {
            return;
        }
        
        $company=$this->companyModel->where("id",$doc['companyid'])->find();
        $exlist= $this->expensesModel->where('docid',$doc['id'])->distinct(true)->field('house_code,remark')->select();
        //$exlisttopssql=$this->expensesModel->where('docid',$doc['id'])->field('min(id)')->group('house_code')->buildSql();
        $exModel=$this->expensesModel;
        $exlistSum=$this->expensesModel->where('id','in',function ($query) use($id) {
            $query->table('es_expenses')->where('docid',$id)->field('min(id)')->group('house_code');
        })->fieldRaw('sum(amount_total) as amount_total,sum(chargearea) as chargearea')->find();
       // $exlistsql=$this->expensesModel->where('docid',$doc['id'])->distinct(true)->field('house_code,remark')->buildSql();
      //  $houselist=$this->houseModel->alias('h')->Join([$exlistsql=>'e'],'e.house_code = h.code')->select();
        // foreach($houselist as $key => $row)
        // {
        //     echo($row['amount_total']);
        // }
       // $houselist=$this->houseModel->where(['code'=>$exlist])->select();
        $this->view->assign('doc', $doc);
        $this->view->assign('company', $company);
        $this->view->assign('exlist', $exlist);
        $this->view->assign('exlistSum', $exlistSum);
      // var_dump($exlisttops);
        return $this->fetch('print');
    }


    public function edit($ids = NULL)
    {
        return parent::modify($ids, 'form');
    }

    public function del($ids = "")
    {
        $where = array(
            'id' => array('in',$ids)
        );
        parent::delete($where);
    }

    public function sumamount()
    {
        $id=input('id',0);
        //UPDATE es_document set totalamount=(SELECT SUM(amount_total) from es_expenses WHERE docid=es_document.id)
       $total= $this->expensesModel->where('docid',$id)->sum('amount_total');
       $this->model->where('id',$id)->update(['totalamount' => $total]);
    }

    public function renew($ids = NULL)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['type'] = 1;
            $this->request->post(array('row' => $params));
            return parent::create();
        }
        return parent::modify($ids, 'renew');
    }

    public function sublet($ids = NULL)
    {
        if ($this->request->isPost()) {
            return parent::create();
        }
        return parent::modify($ids, 'sublet');
    }

    public function updateState()
    {
        $id=input('id','0', 'intval');  
        $verify=input('verify','0', 'intval');  
        $this->model->where('id','=',$id)->update(['verify'=>$verify]);
    }

    public function ListDoc()
    {
       $list=$this->model->field(['id as value,name as label'])->select();
      
       return  json_encode($list);
    }
}
